<template>
  <el-form ref="form" :model="formData" :rules="formRules" label-width="80px" style="width: 30%;margin: 10px 0px 0 214px;">
    <el-form-item label="姓名：" prop="username">
      <el-input v-model="formData.username" />
    </el-form-item>
    <el-form-item label="密码：" prop="password">
      <el-input v-model="formData.password2" type="password" />
    </el-form-item>
    <el-row style="margin-left: 79px">
      <el-button type="primary" :loading="btnLoading" @click="saveInfo">更新</el-button>
    </el-row>
  </el-form>

</template>

<script>
import { getDetailInfo, saveUserInfo } from '@/api/user'
export default {
  name: 'DetailLogin',
  data() {
    return {
      formData: {
        username: '',
        password2: ''
      },
      formRules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 1, max: 6, message: '用户名格式为1-6个字符' }]
      },
      userId: this.$route.params.id,
      btnLoading: false
    }
  },
  created() {
    this.getDetailInfo()
  },
  methods: {
    async getDetailInfo() {
      this.formData = await getDetailInfo(this.userId)
    },
    // 更新用户信息
    async saveInfo() {
      try {
        await this.$refs.form.validate()
        this.btnLoading = true
        await saveUserInfo({ ...this.formData, password: this.formData.password2 ?? '' })
        await this.$store.dispatch('user/getUser')
        this.btnLoading = false
        this.$message.success('更新用户信息成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style >

</style>
